package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.system.service.IRechargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.FeeRecords;
import com.ruoyi.system.mapper.RechargeMapper;
import com.ruoyi.system.domain.Recharge;

/**
 * 门诊充值记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-30
 */
@Service
public class RechargeServiceImpl implements IRechargeService
{
    @Autowired
    private RechargeMapper rechargeMapper;

    /**
     * 查询门诊充值记录
     * 
     * @param id 门诊充值记录主键
     * @return 门诊充值记录
     */
    @Override
    public Recharge selectRechargeById(Long id)
    {
        return rechargeMapper.selectRechargeById(id);
    }

    /**
     * 查询门诊充值记录列表
     * 
     * @param recharge 门诊充值记录
     * @return 门诊充值记录
     */
    @Override
    public List<Recharge> selectRechargeList(Recharge recharge)
    {
        return rechargeMapper.selectRechargeList(recharge);
    }

    /**
     * 新增门诊充值记录
     * 
     * @param recharge 门诊充值记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRecharge(Recharge recharge)
    {
        int rows = rechargeMapper.insertRecharge(recharge);
        insertFeeRecords(recharge);
        return rows;
    }

    /**
     * 修改门诊充值记录
     * 
     * @param recharge 门诊充值记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRecharge(Recharge recharge)
    {
        rechargeMapper.deleteFeeRecordsById(recharge.getId());
        insertFeeRecords(recharge);
        return rechargeMapper.updateRecharge(recharge);
    }

    /**
     * 批量删除门诊充值记录
     * 
     * @param ids 需要删除的门诊充值记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRechargeByIds(Long[] ids)
    {
        rechargeMapper.deleteFeeRecordsByIds(ids);
        return rechargeMapper.deleteRechargeByIds(ids);
    }

    /**
     * 删除门诊充值记录信息
     * 
     * @param id 门诊充值记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRechargeById(Long id)
    {
        rechargeMapper.deleteFeeRecordsById(id);
        return rechargeMapper.deleteRechargeById(id);
    }

    /**
     * 新增门诊消费记录信息
     * 
     * @param recharge 门诊充值记录对象
     */
    public void insertFeeRecords(Recharge recharge)
    {
        List<FeeRecords> feeRecordsList = recharge.getFeeRecordsList();
        Long id = recharge.getId();
        if (StringUtils.isNotNull(feeRecordsList))
        {
            List<FeeRecords> list = new ArrayList<FeeRecords>();
            for (FeeRecords feeRecords : feeRecordsList)
            {
                feeRecords.setId(id);
                list.add(feeRecords);
            }
            if (list.size() > 0)
            {
                rechargeMapper.batchFeeRecords(list);
            }
        }
    }
}
